package com.paas.form.controller;


import com.bpm.feign.model.ProcessCmd;
import com.paas.common.model.RestResponse;
import com.paas.common.util.StringUtil;
import com.paas.common.web.annotion.OperLog;
import com.paas.common.web.controller.BaseController;
import com.paas.common.web.oper.OprLogConstant;
import com.paas.form.dto.FormInfoChangeDto;
import com.paas.form.service.IFormChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;

/**
 * 表单变更
 */
@RestController
@RequestMapping(value = "/form/formChange")
@Api(description = "视图模块", tags = "")
public class FormChangeController extends BaseController {

    @Autowired
    private IFormChangeService formChangeService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ApiOperation(value = "保存表单变更", notes = "保存表单变更")
    @OperLog(operType = OprLogConstant.ADD,operModul = "",operDescribe = "-保存")
    public RestResponse save(@RequestBody FormInfoChangeDto<ProcessCmd> formInfoChangeDto) throws ParseException{
        try {
            formInfoChangeDto.setAppId(getTokenModel().getAppId());
            return formChangeService.saveFormChangeInfo(formInfoChangeDto);
        }catch (Exception e) {
            e.printStackTrace();
            return RestResponse.error("保存失败");
        }
    }

    @RequestMapping(value = "/getDetail",method = RequestMethod.GET)
    @ApiOperation(value = "查询变更明细", notes = "查询变更明细")
    public FormInfoChangeDto getDetail(String logId) throws ParseException {
        FormInfoChangeDto formInfoChangeDto = new FormInfoChangeDto();
        if (StringUtil.isNotEmpty(logId)){
            formInfoChangeDto =  formChangeService.getFormChangeDtoById(logId);
        }
        return formInfoChangeDto;
    }

    @RequestMapping(value = "/approval",method = RequestMethod.POST)
    @ApiOperation(value = "同意变更", notes = "同意变更")
    public RestResponse approvalViewChange( String bussinessKey) throws ParseException {
        if (StringUtil.isNotEmpty(bussinessKey)){
            return formChangeService.approvalFormChange(bussinessKey);
        }else{
            return RestResponse.error("更新失败,必填参数为空");
        }
    }


    @RequestMapping(value = "/oppose",method = RequestMethod.POST)
    @ApiOperation(value = "反对变更", notes = "反对变更")
    public RestResponse oppose( String bussinessKey) throws ParseException {
        if (StringUtil.isNotEmpty(bussinessKey)){
            return formChangeService.opposeFormChange(bussinessKey);
        }else{
            return RestResponse.error("更新失败,必填参数为空");
        }
    }
}